package doan.chuyennganh.activity;

import android.app.ActionBar.LayoutParams;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;

import doan.chuyennganh.base.IRequestQueue;
import doan.chuyennganh.base.RequestQueue;
import doan.chuyennganh.fragment.LeftMenuListFragment;
import doan.chuyennganh.net.Api;
import doan.chuyennganh.net.IApi;
import doan.chuyennganh.util.SharedPreferenceHelper;

public class SlidingMenuActivity extends SlidingFragmentActivity{
	private Menu menu;
	public SharedPreferenceHelper pre;
	private ImageView imvMenu;
	public LeftMenuListFragment mFrag;
	LinearLayout linearLayout;
	private LayoutInflater inflater;
	private View v;
	public IApi daApi;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setTheme(android.R.style.Theme_Holo_NoActionBar);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_base);
		daApi = Api.createInstance(this.getApplicationContext(), getString(R.string.base_url));
		pre = new SharedPreferenceHelper(getApplicationContext());
		imvMenu = (ImageView) findViewById(R.id.main_icon_menu);
		imvMenu.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				getSlidingMenu().toggle();
			}
		});
		linearLayout = (LinearLayout) findViewById(R.id.main_linear_layout);
		SlidingMenu sm = getSlidingMenu();
		sm.setShadowWidthRes(R.dimen.slidingmenu_shadow_width);
		//sm.setShadowDrawable(R.drawable.btn_menu);
		sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sm.setFadeDegree(0.35f);
		sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_MARGIN);
		sm.setMode(SlidingMenu.LEFT);
		setBehindContentView(R.layout.menu_frame);

		if (savedInstanceState == null) {
			FragmentTransaction t = this.getSupportFragmentManager().beginTransaction();
			mFrag = new LeftMenuListFragment();
			t.replace(R.id.menu_frame_left, mFrag);
			t.commit();
		} else {
			mFrag = (LeftMenuListFragment) this.getSupportFragmentManager()
					.findFragmentById(R.id.menu_frame_left);
		}
		inflater = LayoutInflater.from(getApplicationContext());
	}
	
	
	protected void setContentViewX(int id) {
		linearLayout.removeAllViews();
        v =  inflater.inflate(id, null);
		linearLayout.addView(v, LayoutParams.MATCH_PARENT,
				LayoutParams.MATCH_PARENT);
	}
    
    public void setContentViewX(View view) {
        linearLayout.removeAllViews();
        linearLayout.addView(view, LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT);
    }
    
	public RequestQueue getRequestQueue() {
		return ((IRequestQueue) getApplication()).getRequestQueue();
	}
	@Override
	public void onBackPressed() {
		SlidingMenu sm = getSlidingMenu();
		if (sm.isMenuShowing()) {
			getSlidingMenu().toggle();
		} else {
			super.onBackPressed();
		}

	}

	
	@Override
    protected void onPause() {
	    System.gc();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Runtime.getRuntime().gc();
        super.onDestroy();
    }


    public boolean isInternetAccess() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting()) {
			return true;
		}
		return false;
	}
    protected void enableDialogWarning(Context context, String title, String message){
	    AlertDialog.Builder builder;
        builder = new AlertDialog.Builder(context);
        final AlertDialog dialog = builder.create();
        LayoutInflater inflater = LayoutInflater
                .from(context);
        View layout = inflater.inflate(R.layout.dialog_warning_ok,
                null);
        dialog.setView(layout, 0, 0, 0, 0);
        TextView tvTitle = (TextView)layout.findViewById(R.id.dialog_warning_lltheader_tvTitle);
        tvTitle.setText(title);
        TextView tvQuestion = (TextView)layout.findViewById(R.id.dialog_warning_tvQuestion);
        tvQuestion.setText(message);
        Button btnOK = (Button) layout
                .findViewById(R.id.dialog_internet_access_lltfooter_btOK);
        btnOK.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dialog.cancel();
                //finish();
                // System.exit(0);
            }
        });
        dialog.setCancelable(false);
        dialog.show();
	}
    public Menu getMenu() {
		return menu;
	}

	public void setMenu(Menu menu) {
		this.menu = menu;
	}

	public static enum Menu {
		HOME(0), SCANNER(1), LIST(2), EXPORT(3);

		private final int menuChose;

		private Menu(int menu) {
			this.menuChose = menu;
		}

		public int getMenu() {
			return menuChose;
		}
	};

}
